package com.neusoft.neuiotms.dcam.fixed.repository.impl;

import java.util.List;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import com.neusoft.bizcore.web.jpa.repository.AbstractRepository;
import com.neusoft.bizcore.web.support.Searchable;
import com.neusoft.neuiotms.dcam.fixed.model.AhAssetCard;
import com.neusoft.neuiotms.dcam.fixed.model.QAhAssetCard;
import com.neusoft.neuiotms.dcam.fixed.repository.CustomizedAhAssetCardRepository;
import com.querydsl.core.BooleanBuilder;
import com.querydsl.jpa.impl.JPAQuery;

public class CustomizedAhAssetCardRepositoryImpl extends AbstractRepository implements CustomizedAhAssetCardRepository {

    @Override
    public List<AhAssetCard> search(final Searchable searchable) {
        final QAhAssetCard qQAhAssetCard = QAhAssetCard.ahAssetCard;
        final BooleanBuilder where = this.buildWhere(searchable);
        final JPAQuery<AhAssetCard> query = this.query().selectFrom(qQAhAssetCard);
        query.where(where);
        return query.fetch();
    }

    @Override
    public Page<AhAssetCard> search(final Pageable pageable, final Searchable searchable) {
        final QAhAssetCard qQAhAssetCard = QAhAssetCard.ahAssetCard;
        final BooleanBuilder where = this.buildWhere(searchable);
        final JPAQuery<AhAssetCard> query = this.query().selectFrom(qQAhAssetCard);
        query.where(where);
        return super.search(query, pageable);
    }

    @Override
    protected Class<?> getModelClass() {
        return AhAssetCard.class;
    }

    private BooleanBuilder buildWhere(final Searchable searchable) {
        final BooleanBuilder where = new BooleanBuilder();
        final QAhAssetCard qQAhAssetCard = QAhAssetCard.ahAssetCard;
        // 名称检索
        where.and(super.containsStr(qQAhAssetCard.code, searchable, "code"));
        return where;
    }

}
